/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.shadow.rule.attribute;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
import org.apache.shardingsphere.shadow.rule.ShadowDataSourceRule;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Shadow data source mapper rule attribute.
 */
@HighFrequencyInvocation
@RequiredArgsConstructor
public final class ShadowDataSourceMapperRuleAttribute implements DataSourceMapperRuleAttribute {
    
    private final Map<String, ShadowDataSourceRule> dataSourceRules;
    
    @HighFrequencyInvocation
    @Override
    public Map<String, Collection<String>> getDataSourceMapper() {
        Map<String, Collection<String>> result = new LinkedHashMap<>(dataSourceRules.size(), 1F);
        for (Entry<String, ShadowDataSourceRule> entry : dataSourceRules.entrySet()) {
            result.put(entry.getKey(), Arrays.asList(entry.getValue().getProductionDataSource(), entry.getValue().getShadowDataSource()));
        }
        return result;
    }
}
